<?php

App::uses('AuthComponent', 'Controller/Component');
App::uses('CakeEmail', 'Network/Email');
class User extends AppModel{
	public $name = 'User';
	public $useTable = 'users';
	
	public $hasMany = 'Post';
	public $displayField = 'username';
	
	public $validate = array(
		'username' => array(
			'alphaNumeric' => array(
				'rule' => 'alphaNumeric',
				'required' => TRUE,
				'message' => 'Username can contain only letters and numbers'),
			'between' => array(
				'rule' => array('between', 5, 20),
				'message' => 'Username must be 5-20 letters long'),
			'isUnique' => array(
				'rule' => 'isUnique',
				'message' => 'Such username already exists'),
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'message' => 'Please fill in the username')
			),
		'email' => array(
			'email' => array(
				'rule' => 'email',
				'message' => 'Wrong email address'),
			'isUnique' => array(
				'rule' => 'isUnique',
				'message' => 'Such email already exists'),
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'message' => 'Please fill in the email')
			),
		'password' => array(
			'rule' => array('minLength', 8),
			"message" => "Password should be at least 8 characters long"
			),
		'confirm_password' => array(
      		'required'=>'notEmpty',
      		'match'=>array(
        		'rule' => 'validatePasswdConfirm',
        		'message' => 'Passwords do not match')
			),
		'rules' => array(
              'rule' => array('comparison', '!=', 0),
              'required' => true,
              'message' => 'You must agree to the terms of use',
              'on' => 'create')
		);

	public function beforeSave($options = array()) {
		parent::beforeSave($options);
		
    	if (isset($this->data[$this->alias]['password'])) {
        	$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    	}
    	return true;
    }
	
	public function validatePasswdConfirm($data)
  	{
    	if ($this->data['User']['password'] !== $data['confirm_password']){
      		return false;
    	}
    	return true;
  	}
}
	



?>


